package com.lai.hospital.handler;


import com.alibaba.fastjson2.JSON;
import com.lai.hospital.vo.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.lai.hospital.constant.CommonConst.APPLICATION_JSON;
import static com.lai.hospital.constant.SecurityConst.INSUFFICIENT_PRIVILEGES;

/**
 * @author : lai
 * @version V1.0
 * @Project: hospital-springboot
 * @Package com.lai.hospital.handler
 * @Description: 访问拒绝处理器（用户权限处理）
 * @date Date : 2022年11月16日 21:01
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setContentType(APPLICATION_JSON);
        //权限不足
        httpServletResponse.getWriter().write(JSON.toJSONString(Result.fail(INSUFFICIENT_PRIVILEGES)));
    }
}
